package com.wyg.epuser.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wyg.epmysql.common.page.PageReq;
import com.wyg.epmysql.common.page.PageRes;
import com.wyg.epcommonbase.common.ApiResult;
import com.wyg.epcommonbase.common.annotation.Login;
import com.wyg.epcommonbase.common.constant.SessionCST;
import com.wyg.epcommonbase.common.util.AssertUtil;
import com.wyg.epcommonbase.common.util.BeanMapper;
import com.wyg.epuser.entity.User;
import com.wyg.epuser.service.UserEntityService;
import com.wyg.epuser.service.UserService;
import com.wyg.epuser.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import org.apache.poi.util.TempFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WangYongGan
 * @since 2021-11-20
 */
@Api(tags = "用户")
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserEntityService userEntityService;
    @Autowired
    UserService userService;


    @ApiOperation("test")
    @GetMapping("test")
    public String test(HttpServletResponse response) {
        return "成功" + RequestUtil.getSession().getId();
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ApiResult login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
        User userByName = userService.selectByName(user.getUsername());
        if (userByName == null)
            return ApiResult.error(101, "没有该用户");
        if (!userByName.getPassword().equals(user.getPassword()))
            return ApiResult.error(102, "密码错误");
        request.getSession().setAttribute(SessionCST.USER, userByName);
        return ApiResult.ok().data(userByName).message(RequestUtil.getSession().getId());
    }

    @Login
    @ApiOperation("获得用户信息")
    @GetMapping("getInfo")
    public ApiResult getInfo() {
        final User user = RequestUtil.getUser();
        if (user == null)
            return ApiResult.ok().message(RequestUtil.getSession().getId());
        return ApiResult.ok().data(user);
    }

    @ApiOperation("register")
    @PostMapping("register")
    public ApiResult register(User user) {
        userService.register(user);
        return ApiResult.ok();
    }


    @Login
    @ApiOperation("退出登录")
    @GetMapping("loginOut")
    public ApiResult loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute(SessionCST.USER);
        request.getSession().invalidate();
        return ApiResult.ok();
    }

    @Login
    @ApiOperation("修改用户信息")
    @PostMapping("updateInfo")
    public ApiResult updateInfo(@RequestBody User user) {
        AssertUtil.isTrue(StringUtils.isEmpty(user.getEmail()), 111, "邮箱不能为空");
        final User login = RequestUtil.getUser();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        final User byId = userService.getById(login.getId());
        wrapper.eq("email", user.getEmail());
        User temp = userService.getOne(wrapper);
        AssertUtil.isTrue(temp != null && temp.getId() != byId.getId(), 113, "邮箱已存在");
        wrapper.clear();
        wrapper.eq("username", user.getUsername());
        AssertUtil.isTrue((temp = userService.getOne(wrapper)) != null && !temp.getUsername().equals(byId.getUsername()), 109, "用户名已存在");
        user.setId(login.getId());
        userService.updateById(user);
        return ApiResult.ok();
    }


    @Login
    @ApiOperation("获取用户列表--分页")
    @GetMapping("getUserList")
    public ApiResult getUserList(PageReq req) {
        final Page page = req.toPage();
        userService.page(page);
        PageRes pageRes = new PageRes();
        BeanMapper.copy(page, pageRes);
        return ApiResult.ok().data(pageRes);
    }

    @Login
    @ApiOperation("删除用户")
    @DeleteMapping()
    public ApiResult deleteUser(@RequestParam int uid) {
        userService.removeById(uid);
        return ApiResult.ok();
    }
}

